﻿// I got this code from http://mrrichie.spaces.live.com/blog/cns!DD16C3F34F4D913E!1803.entry?wa=wsignin1.0&sa=839940878
// there was no copyright notice but all credit goes to Richie Carmichael
// 

using System;
using System.Windows.Data;

namespace Phone7ActionPack.ValueConverters
{
    public enum CoordinateType { longitude, latitude };
    public class DDtoDMSValueConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            string param = "";
            if (parameter == null)
                return null;
            param = parameter.ToString();
            double coordinate;
            if (value == null)
                return value;
            if (double.TryParse(value.ToString(), out coordinate))
            {
                // Set flag if number is negative
                bool neg = coordinate < 0d;

                // Work with a positive number
                coordinate = Math.Abs(coordinate);

                // Get d/m/s components
                double d = Math.Floor(coordinate);
                coordinate -= d;
                coordinate *= 60;
                double m = Math.Floor(coordinate);
                coordinate -= m;
                coordinate *= 60;
                double s = Math.Round(coordinate);

                // Create padding character
                char pad;
                char.TryParse("0", out pad);

                // Create d/m/s strings
                string dd = d.ToString();
                string mm = m.ToString().PadLeft(2, pad);
                string ss = s.ToString().PadLeft(2, pad);

                // Append d/m/s
                string dms = string.Format("{0}°{1}'{2}\"", dd, mm, ss);

                // Append compass heading
                switch (param)
                {
                    case "longitude":
                        dms += neg ? "W" : "E";
                        if (coordinate > 90.0D || coordinate < -90.0D)
                        {
                            throw new ArgumentOutOfRangeException(
                                "coordinate",
                                "Latitudes fall between -90 (South Pole) and +90 (North Pole)");
                        }
                        break;
                    case "latitude":
                        dms += neg ? "S" : "N";
                        if (coordinate > 180.0D || coordinate < -180.00)
                        {
                            throw new ArgumentOutOfRangeException(
                                "coordinate",
                                "Longitudes cannot be greater than +180 or less than -180 (International Date Line)");
                        }
                        break;
                }
                // Return formated string
                if (double.IsNaN(coordinate))
                    return string.Empty;
                else
                    return dms;
            }
            return string.Empty;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
